package com.mpj.yolov12

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.core.app.ActivityCompat
import com.blankj.utilcode.util.LogUtils

class DataActivity : Activity(), OnUIProcessListener {

    private lateinit var statusText: TextView
    private lateinit var logText: TextView
    private lateinit var startButton: Button
    private lateinit var stopButton: Button
    private lateinit var handler: Handler

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_data)
        handler = Handler(Looper.getMainLooper())
        // 初始化UI组件
        initView()
        // 请求权限
        requestPermission()
        // 创建UDP监听器(默认端口8888)
        DataStreamProcessHelper.instance.createUdpBroadcastListener()
    }

    private fun initView() {
        // 初始化UI组件
        statusText = findViewById(R.id.statusText)
        logText = findViewById(R.id.logText)
        startButton = findViewById(R.id.startButton)
        stopButton = findViewById(R.id.stopButton)

        // 设置按钮点击事件
        startButton.setOnClickListener {
            DataStreamProcessHelper.instance.startUdpBroadcastListening()
            updateUiState(true)
        }

        stopButton.setOnClickListener {
            DataStreamProcessHelper.instance.stopUdpBroadcastListening()
            updateUiState(false)
        }

        findViewById<Button>(R.id.clearButton).setOnClickListener {
            logText.text = ""
        }
    }

    private fun requestPermission() {
        ActivityCompat.requestPermissions(
            this,
            arrayOf(
                android.Manifest.permission.INTERNET,
                android.Manifest.permission.ACCESS_NETWORK_STATE,
                android.Manifest.permission.ACCESS_WIFI_STATE,
                android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.ACCESS_COARSE_LOCATION,
            ),
            0
        )
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(
                    Manifest.permission.NEARBY_WIFI_DEVICES,
                ),
                0
            )
        }
    }

    override fun onResume() {
        super.onResume()
        WifiHelper.instance.openWifiManager(this)
    }

    override fun onPause() {
        super.onPause()
        WifiHelper.instance.releaseWifiManager()
    }

    // 更新UI状态
    private fun updateUiState(isListening: Boolean) {
        statusText.text = if (isListening) "正在监听" else "未监听"
        statusText.setTextColor(if (isListening) Color.GREEN else Color.RED)

        startButton.isEnabled = !isListening
        stopButton.isEnabled = isListening
    }

    override fun onDestroy() {
        super.onDestroy()
        DataStreamProcessHelper.instance.stopUdpBroadcastListening()
    }

    override fun onUDPInfoReceived(udpInfo: UDPInfo) {
        // 跳转到SecActivity
        val intent = Intent(this, SecActivity::class.java).apply {
            putExtra("ip", udpInfo.senderIp)
            putExtra("port", udpInfo.senderPort)
        }
        startActivity(intent)
    }

    override fun updateReceivedDataStatus(status: String, isListening: Boolean) {
        LogUtils.d("updateReceivedDataStatus", "status: $status" + "isListening: $isListening")
    }
}
